package com.blb.service.impl;

import com.blb.entity.Brand;
import com.blb.entity.Comimg;
import com.blb.entity.Commodity;
import com.blb.entity.Goodstype;
import com.blb.mapper.CommodityMapper;
import com.blb.service.IComimgService;
import com.blb.service.ICommodityService;
import com.blb.service.IGoodstypeService;
import com.blb.utils.NowUserUtils;
import com.blb.utils.UUIDUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author txy
 * @since 2019-07-11
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements ICommodityService {

    @Autowired
    private CommodityMapper commodityMapper;

    @Autowired
    private IGoodstypeService goodstypeService;

    @Autowired
    private IComimgService comimgService;

    /**
     * 添加商品
     * 添加商品详情
     * 添加商品与类别关联表
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Commodity commodity, String dimg, String comtypeName) throws Exception {

        String commodityid = UUIDUtils.getID();
        //添加商品信息
        commodity.setId(commodityid);
        commodity.setCreateTime(new Date());
        commodity.setCreateUser(NowUserUtils.getNowUser().getUsername());
        commodity.setPutawayTime(new Date());
        commodityMapper.insert(commodity);

        //添加商品类别与商品信息关联表
        String[] comtypeNames = comtypeName.split(",");
        for (String comname : comtypeNames) {
            Goodstype goodstype = new Goodstype();
            goodstype.setComId(commodity.getId());
            goodstype.setTypeId(comname);
            goodstypeService.save(goodstype);
        }

        //添加商品详细信息
        String[] dimgs = dimg.split(",");
        for (int i = 0; i < dimgs.length; i++) {
            Comimg comimg = new Comimg();
            comimg.setComId(commodityid);
            comimg.setImg(dimgs[i]);
            comimg.setSeq(i + 1);
            comimgService.save(comimg);
        }

    }

    /**
     * 修改商品
     * 修改商品详情
     * 修改商品与类别关联表
     *
     * @param commodity
     * @param dimg
     * @param comtypeName
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Commodity commodity, String dimg, String comtypeName) throws Exception {
        commodity.setUpdateTime(new Date());
        commodity.setUpdateUser(NowUserUtils.getNowUser().getUsername());

        //1、先删除
        //删除商品类型关联信息
        goodstypeService.remove(new QueryWrapper<Goodstype>().eq("com_id", commodity.getId()));

        //删除商品详细信息
        comimgService.remove(new QueryWrapper<Comimg>().eq("com_id", commodity.getId()));

        //2、再添加
        //添加商品信息
        commodityMapper.update(commodity, new QueryWrapper<Commodity>().eq("id", commodity.getId()));

        //添加商品类别与商品信息关联表
        String[] comtypeNames = comtypeName.split(",");
        for (String comname : comtypeNames) {
            Goodstype goodstype = new Goodstype();
            goodstype.setComId(commodity.getId());
            goodstype.setTypeId(comname);
            goodstypeService.save(goodstype);
        }

        //添加商品详细信息
        String[] dimgs = dimg.split(",");
        for (int i = 0; i < dimgs.length; i++) {
            Comimg comimg = new Comimg();
            comimg.setComId(commodity.getId());
            comimg.setImg(dimgs[i]);
            comimg.setSeq(i + 1);
            comimgService.save(comimg);
        }


    }

    /**
     * 删除商品
     * 删除商品详情
     * 删除商品与类别关联表
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<String> ids) throws Exception {
        //删除商品
        commodityMapper.deleteBatchIds(ids);
        for (String id : ids) {
            //删除商品类别与商品信息关联表数据
            goodstypeService.remove(new QueryWrapper<Goodstype>().eq("com_id", id));

            //删除商品详情
            comimgService.remove(new QueryWrapper<Comimg>().eq("com_id", id));
        }

    }

    /**
     * 根据上架时间排序，获取前七条数据
     * @return
     */
    @Override
    public List<Commodity> getNewCommodity() {
        List<Commodity> newCommodity = commodityMapper.getNewCommodity();
        return newCommodity;
    }

    /**
     * 根据销量排序，获取前六条数据
     * @return
     */
    @Override
    public List<Commodity> getMroeCommodity() {
        List<Commodity> moreCommodity = commodityMapper.getMoreCommodity();
        return moreCommodity;
    }

    /**
     * 根据商品类别查询商品信息
     * @param id
     * @return
     */
    @Override
    public List<Commodity> getCommodityByTypeId(String id) {
        List<Commodity> detailCommodity = commodityMapper.getDetailCommodity(id);
        return detailCommodity;
    }


}
